<?php
	/*
	*https://github.com/nicolasff/phpredis/downloads
	
	
	
	*/
	class AdCache {		// 
			var $redis;				//redis obj
			var $stats;	
			var $config;
			/*  $config is array('ip'=>'127.0.0.1','port'=>'6379')
			 *	$config_cache_type  is  
			 *
			 */
			function __construct($config){
				$this->config=$config;
				$this->redis = new Redis();
				
				try{ 
					$connectstatus = $this->redis->connect($this->config['m']['ip'],$this->config['m']['port']);
				}catch(Exception $e){
					$connectstatus = $this->redis->connect($this->config['s']['ip'],$this->config['s']['port']);
				}
				if($connectstatus){
					$this->redis->setOption(Redis::OPT_SERIALIZER,Redis::SERIALIZER_NONE);
					$this->redis->setOption(Redis::OPT_PREFIX,'ad:');
					$this->redis->stats=true;
				}else{
					$this->redis->stats=false;
					return NUll;
				}
			}
			
			
			function getCache($ad_place_id,$nowtime){
				if($ad_place_id && $this->redis->stats){	
					if($ad_push_id_arr=$this->redis->zRangeByScore($ad_place_id, 0, $nowtime)){
						if($ad_content_arr_s=$this->redis->getMultiple($ad_push_id_arr)){
							foreach ($ad_content_arr_s as $v){
							
								$ad_content_arr[]=unserialize($v);
							}
						return $ad_content_arr;
						}
					}else{
						return false;
					}
				}else{
					return false;
				} 
			}
			
			function setCache($ad_place_id,$time, $push_ad,$expire=false){	 
				if($ad_place_id&&$push_ad&&$time   && $this->redis->stats ){
						if($this->redis->zAdd($ad_place_id, $time, $ad_place_id.':'.$push_ad['id']) ){
							
							if($this->redis->set($ad_place_id.':'.$push_ad['id'],  serialize($push_ad))){
								if($expire){
									$this->redis->EXPIRE($ad_place_id,3600);
									$this->redis->EXPIRE($ad_place_id,3600);
								}
								return true ;
							}else{
								return false ;
							}
						}	
				}else{
					return false;
				}  
			}
			
			function deleteCache($ad_place_id, $push_id ){	
				if($ad_place_id && $push_id && $this->redis->stats){
					 if( $this->redis->zDelete($ad_place_id, $ad_place_id.':'.$push_id)){
					  return $this->redis->del($ad_place_id.':'.$push_id);
					 }
				}else{
					return false;
				}  
				
			}
			 
			function clearCache($ad_place_id ){	 
				if( $ad_place_id>=0 && $this->redis->stats){
					if($this->redis->delete($ad_place_id)){
							if($keys_arr=$this->redis->getKeys($$ad_place_id.':*')){
								$keys_arr = str_replace('ad:cache:',  '', $keys_arr);
								return $this->redis->del($keys_arr);
							}
						} 
				}else{
					return false;
				}  
			}
			 
			 function getRedis($key ){	 
					//$this->connect();
					return $this->redis->GET($key );	  
				 		 
			}
			
			 function setRedis($key,$value){	 
					//$this->connect();
					return $this->redis->SET($key,$value );	
							 
			}
			 
			 function mgetRedis($array ){	 
					//this->connect();
					return $this->redis->MGET($array );	  
				 		 
			}
			 
			 function msetRedis($array){	 
					//$this->connect();
					return $this->redis->MSET($array );	
							 
			}
			function __destruct() {
				return $this->redis->close();	
			}
	}
 
 
?>
